package com.gfm.story.service.dict;

import com.gfm.story.dao.DictionaryDetailDao;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.DictionaryDetail;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**

* @Description:    数据字典子集逻辑控制层

* @Author:         ly

* @CreateDate:     2019/3/19 0019 下午 2:34

* @UpdateUser:     yc

* @UpdateDate:     2019/3/19 0019 下午 2:34

* @UpdateRemark:   修改内容

* @Version:        1.0

*/
@Service
public class DictionaryDetailService {

    @Resource
    private DictionaryDetailDao sysDictionaryDetailDao;

    /**
     * 获取所有子集数据字典列表
     *
     * @param map
     * @return
     */
    public ResultBean selectSysDictionaryDetailList(Map<String, Object> map) {

        return ResultBean.getSuccess(sysDictionaryDetailDao.selectSysDictionaryDetailList(map));
    }

    /**
     * 获取所有子集数据字典列表
     *
     * @param dictCode
     * @return
     */
    public ResultBean SysDictionaryDetailListByDictCode(String dictCode,String paltCode) {

        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("dictCode",dictCode);
        map1.put("platCode",null);
        return ResultBean.getSuccess(sysDictionaryDetailDao.selectSysDictionaryDetailByDictCode(map1));
    }

    /**
     * 新增或者修改数据字典子集信息
     *
     * @param sysDictionaryDetail
     * @return
     */
    public ResultBean addOrEdit(DictionaryDetail sysDictionaryDetail) {
        if (null != sysDictionaryDetail) {

            DictionaryDetail db = null;
            try {
                DictionaryDetail param = new DictionaryDetail();
                param.setDictDatailCode(sysDictionaryDetail.getDictDatailCode());
                param.setDictId(sysDictionaryDetail.getDictId());
                db = this.sysDictionaryDetailDao.selectDetail(param);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultBean.getError("服务器开小差!");
            }
            /**
             * 新增或者修改的时候创建时间,修改时间以及默认删除状态不可修改
             */
            sysDictionaryDetail.setIsDelete(null);
            sysDictionaryDetail.setCreateTime(null);
            sysDictionaryDetail.setUpdateTime(null);
            /**
             * 前端页面传入数据字典的ID为空则表示新增
             */
            if (sysDictionaryDetail.getId() == null) {
                /**
                 * 根据数据字典子集唯一Code查找是否已经存在
                 */
                if (db != null) {
                    return ResultBean.getError("数据字典已存在!");
                }
                sysDictionaryDetail.insert();
                return ResultBean.getSuccess("操作成功!");

            } else {
                /**
                 * 如果ID为空则为编辑
                 * 数据字典唯一编码不能修改
                 */
                sysDictionaryDetail.setDictDatailCode(null);
                sysDictionaryDetail.updateById();
                return ResultBean.getSuccess("操作成功!");
            }
        } else {
            return ResultBean.getError("参数为空");
        }

    }

    /**
     * 删除数据字典子集,支持批量删除
     *
     * @param id
     * @return
     */
    public ResultBean remove(Long id) {
        if (StringUtils.isNotBlank(id.toString())) {
            DictionaryDetail sysDictionaryDetail = new DictionaryDetail();
            sysDictionaryDetail.setId(id);
            sysDictionaryDetail.deleteById();
            return ResultBean.getSuccess("删除成功!");
        } else {
            return ResultBean.getError("参数为空");
        }

    }
}
